<template>
  <transition name="fade">
    <div v-if="visible" class="message" :class="type">
      {{ content }}
    </div>
  </transition>
</template>

<script>
export default {
  name: 'Message',
  data() {
    return {
      visible: false,
      content: '',
      type: 'info',
      duration: 3000
    }
  },
  methods: {
    show({ content, type = 'info', duration = 3000 }) {
      this.visible = true
      this.content = content
      this.type = type
      this.duration = duration

      if (this.duration > 0) {
        setTimeout(() => {
          this.hide()
        }, this.duration)
      }
    },
    hide() {
      this.visible = false
    }
  }
}
</script>

<style scoped>
.message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  z-index: 10000;
}

.success {
  background-color: #67c23a;
}

.warning {
  background-color: #e6a23c;
}

.error {
  background-color: #f56c6c;
}

.info {
  background-color: #909399;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translate(-50%, -20px);
}
</style>
